package com.dai.chun.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.dai.chun.dao.dao.PerformanceDao;
import com.dai.chun.dao.model.Performance;
import com.dai.chun.dao.model.Wage;
import com.dai.chun.dbc.DBManager;
import com.dai.chun.util.PageSplitUtil;

public class PerformanceDaoImpl implements PerformanceDao {
	private DBManager manager = DBManager.getInstance();

	public List<Performance> listPerformance(PageSplitUtil psu) {
		String sql = "select * from t_performance limit ?,?";
		List<Performance> performances = new ArrayList<Performance>();
		Connection conn = null;
		ResultSet res = null;
		try {
			conn = manager.getConnection();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		PreparedStatement preparedStatement = manager.createPRPstmt(conn, sql);
		try {
			preparedStatement.setInt(1, psu.getStart());
			preparedStatement.setInt(2, psu.getEnd());
			res = preparedStatement.executeQuery();

			while (res.next()) {
				Performance performance = new Performance();
				performance.setPerformance_id(res.getInt("performance_id"));
				performance.setPerformance_emp_id(res
						.getInt("performance_emp_id"));
				performance.setPerformance_emp_name(res
						.getString("performance_emp_name"));
				performance.setPerformance_date(res
						.getString("performance_date"));
				performance.setPerformance_reward(res
						.getFloat("performance_reward"));
				performance.setPerformance_penalty(res
						.getFloat("performance_penalty"));
				performance.setPerformance_reason(res
						.getString("performance_reason"));
				performances.add(performance);
			}
		} catch (Exception e) {
		} finally {
			manager.closeConn(conn, preparedStatement, res);
		}
		return performances;
	}

	public Boolean save(Performance performance) throws SQLException {
		boolean flag = false;
		String sql = "insert into t_performance (performance_emp_id,performance_emp_name,performance_date,performance_treat_type,performance_treat_amount,"
				+ "performance_reward,performance_penalty,performance_reason) value (?,?,?,?,?,?,?,?)";
		Connection conn = manager.getConnection();
		PreparedStatement preparedStatement = manager.createPRPstmt(conn, sql);
		try {
			preparedStatement.setInt(1, performance.getPerformance_emp_id());
			preparedStatement.setString(2,
					performance.getPerformance_emp_name());
			preparedStatement.setString(3, performance.getPerformance_date());
			preparedStatement.setString(4,
					performance.getPerformance_treat_type());
			preparedStatement.setFloat(5,
					performance.getPerformance_treat_amount());
			preparedStatement.setFloat(6, performance.getPerformance_reward());
			preparedStatement.setFloat(7, performance.getPerformance_penalty());
			preparedStatement.setString(8, performance.getPerformance_reason());
			int count = preparedStatement.executeUpdate(); //
			if (count > 0) {
				flag = true;
			}
		} catch (Exception e) {
		} finally {
			manager.closeConn(conn, preparedStatement);
		}
		return flag;
	}

	@Override
	public int findAllCount() throws SQLException {
		int ret = 0;
		String sql = "select count(*) from t_performance";
		Connection conn = manager.getConnection();
		java.sql.Statement sm = conn.createStatement();
		ResultSet rs = sm.executeQuery(sql);
		if (rs.next()) {
			ret = rs.getInt(1);
		}
		manager.closeConn(conn, sm, rs);
		return ret;
	}

	@Override
	public int delPerformance(int performance_id) throws SQLException {
		int ret = 0;
		String sql = "delete from t_performance where performance_id=?";
		Connection conn = manager.getConnection();
		PreparedStatement preparedStatement = manager.createPRPstmt(conn, sql);
		preparedStatement.setInt(1, performance_id);
		ret = preparedStatement.executeUpdate();
		manager.closeConn(conn, preparedStatement);
		return ret;

	}

	@Override
	public int updatePerformance(Performance performance) throws SQLException {
		int ret = 0;
		String sql = "update t_performance set performance_emp_id=?,performance_emp_name=?,"
				+ "performance_date=?,performance_reward=?,performance_penalty=? ,performance_reason=? where performance_id=?";
		Connection conn = manager.getConnection();
		PreparedStatement preparedStatement = manager.createPRPstmt(conn, sql);
		try {
			preparedStatement.setInt(1, performance.getPerformance_emp_id());
			preparedStatement.setString(2,
					performance.getPerformance_emp_name());
			preparedStatement.setString(3, performance.getPerformance_date());
			preparedStatement.setFloat(4, performance.getPerformance_reward());
			preparedStatement.setFloat(5, performance.getPerformance_penalty());
			preparedStatement.setString(6, performance.getPerformance_reason());
			preparedStatement.setInt(7, performance.getPerformance_id());
			int count = preparedStatement.executeUpdate(); //
			if (count > 0) {
				ret = 1;
			}
		} catch (Exception e) {
		} finally {
			manager.closeConn(conn, preparedStatement);
		}

		return ret;
	}

	@Override
	public Performance findPerformanceById(int performance_id) {
		String sql = "select * from t_performance where performance_id=?";
		Performance performance = new Performance();
		Connection conn = null;
		PreparedStatement preparedStatement = null;
		ResultSet res = null;
		try {
			conn = manager.getConnection();
			preparedStatement = manager.createPRPstmt(conn, sql);
			preparedStatement.setInt(1, performance_id);
			res = preparedStatement.executeQuery();
			while (res.next()) {
				performance.setPerformance_id(res.getInt("performance_id"));
				performance.setPerformance_emp_id(res
						.getInt("performance_emp_id"));
				performance.setPerformance_emp_name(res
						.getString("performance_emp_name"));
				performance.setPerformance_date(res
						.getString("performance_date"));
				performance.setPerformance_reward(res
						.getFloat("performance_reward"));
				performance.setPerformance_penalty(res
						.getFloat("performance_penalty"));
				performance.setPerformance_reason(res
						.getString("performance_reason"));
			}
		} catch (Exception e) {
		} finally {
			manager.closeConn(conn, preparedStatement, res);
		}
		return performance;
	}

	@Override
	public List<Performance> listPerformanceByDeptId(PageSplitUtil psu,
			int dept_id) {
		String sql = "select * from t_performance  where performance_dept_id=? limit ?,?";
		List<Performance> performances = new ArrayList<Performance>();
		Connection conn = null;
		ResultSet res = null;
		try {
			conn = manager.getConnection();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		PreparedStatement preparedStatement = manager.createPRPstmt(conn, sql);
		try {
			preparedStatement.setInt(1, dept_id);
			preparedStatement.setInt(2, psu.getStart());
			preparedStatement.setInt(3, psu.getEnd());
			res = preparedStatement.executeQuery();

			while (res.next()) {
				Performance performance = new Performance();
				performance.setPerformance_id(res.getInt("performance_id"));
				performance.setPerformance_emp_id(res
						.getInt("performance_emp_id"));
				performance.setPerformance_emp_name(res
						.getString("performance_emp_name"));
				performance.setPerformance_date(res
						.getString("performance_date"));
				performance.setPerformance_reward(res
						.getFloat("performance_reward"));
				performance.setPerformance_penalty(res
						.getFloat("performance_penalty"));
				performance.setPerformance_reason(res
						.getString("performance_reason"));
				performances.add(performance);
			}
		} catch (Exception e) {
		} finally {
			manager.closeConn(conn, preparedStatement, res);
		}
		return performances;
	}

	@Override
	public List<Performance> listPerformance(int wage_emp_id, String wage_month) {
		String sql = "select * from t_performance  where performance_month=? and performance_emp_id=?";
		List<Performance> performances = new ArrayList<Performance>();
		Connection conn = null;
		ResultSet res = null;
		try {
			conn = manager.getConnection();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		PreparedStatement preparedStatement = manager.createPRPstmt(conn, sql);
		try {
			preparedStatement.setString(1, wage_month);
			preparedStatement.setInt(2, wage_emp_id);
			res = preparedStatement.executeQuery();

			while (res.next()) {
				Performance performance = new Performance();
				performance.setPerformance_id(res.getInt("performance_id"));
				performance.setPerformance_emp_id(res
						.getInt("performance_emp_id"));
				performance.setPerformance_emp_name(res
						.getString("performance_emp_name"));
				performance.setPerformance_date(res
						.getString("performance_date"));
				performance.setPerformance_reward(res
						.getFloat("performance_treat_type"));
				performance.setPerformance_reward(res
						.getFloat("performance_treat_amount"));
				// performance.setPerformance_penalty(res.getFloat("performance_penalty"));
				performance.setPerformance_reason(res
						.getString("performance_reason"));
				performances.add(performance);
			}
		} catch (Exception e) {
		} finally {
			manager.closeConn(conn, preparedStatement, res);
		}
		return performances;
	}

}
